/**
 * Follower Action file
 */

var Action = new Function();

Action.prototype.init = function(req, res){
	switch(req.method) {
		case 'GET': this.get(req, res); break;
		case 'DELETE': this.delete(req, res); break;
		default: this.other(req, res); break;
	}
};

Action.prototype.get = function(req, res){
	var query = require('url').parse(req.url, true).query;
	var id = query.id;
	var getFollow = new (Services('getFollower'))(id, {
		success: function(data){
			res.send(JSON.stringify(data));
			res.end();
		},
		error: function(code, message){
			throw new Exception(code, message, function(){
				res.send(this.toString());
				res.end();
			});
		}
	});
};

Action.prototype.delete = function(req, res){
	var query = require('url').parse(req.url,true).query;
	var token = query.token;
	var followerId = req.body.followerId;
	var authen = new (Services('authen'))(token, {
		success: function(agent){
			var delFollow = new (Services('deleteFollow'))(agent.id, followerId, {
				success: function(data){
					res.send(data);
					res.end();
				},
				error: function(code, message){
					throw new Exception(code, message, function(){
						res.send(this.toString());
						res.end();
					});
				},
			});
		},
		error: function(code, message){
			throw new Exception(code, message, function(){
				res.send(this.toString());
				res.end();
			});
		}
	});
};

Action.prototype.other = function(req, res){
	res.writeHead(404, {
		'Content-Type': 'text/plain',
		'Trailer': 'Content-MD5'
	});
	res.end();
};

module.exports = Action;